<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>

</head>
<script type="text/javascript">
	$(function (){
		findall()
		$("#brandid").select2();
		$("#specid").select2();


	})
	//拼接显示页面
	function findall(){
		$.ajax({
			url:"/templateController/findall?name=" + $("#nameid").val(),
			type: "pot",
			dataType: "json",
			async: false,
			success:function (list){
				var templateList = list.t;
				var brandIds = "";
				var specIds = "";
				var Attribute = "";
				var html = "";
				for (var i = 0; i < templateList.length; i++) {
					var brandId = templateList[i].brandIds;
					var brandIdsList = JSON.parse(brandId);
					var specId = templateList[i].specIds;
					var specIdsList = JSON.parse(specId);
					var cai = templateList[i].customAttributeItems;
					var AttributeList = JSON.parse(cai);
					html += "<tr>"
					html +=	"<td><input type='checkbox' name='check' value='" + templateList[i].id + "'></td>"
					html += "<td>" + templateList[i].id + "</td>"
					html += "<td>" + templateList[i].name + "</td>"
					html += "<td>"
							for(var j = 0;j < brandIdsList.length;j++){
								brandIds += "," + brandIdsList[j].text;
							}
							brandIds = brandIds.substring(1);
							html += brandIds
							brandIds = ""
					html += "</td>"
					html += "<td>"
							for(var k = 0;k < specIdsList.length;k++){
								specIds += "," + specIdsList[k].text;
							}
							specIds = specIds.substring(1)
							html += specIds
							specIds = ""
					html += "</td>"
					html += "<td>"
							for(var x = 0;x < AttributeList.length;x++){
								Attribute += "," + AttributeList[x].text;
							}
							Attribute = Attribute.substring(1)
							html += Attribute
							Attribute = ""
					html +="</td>"
					html += '<td className="text-center">'
					html += '<button type="button" onclick="echo(' + templateList[i].id + ')" className="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal2">修改'
					html += '</button></td></tr>'
				}
				$("#findall").html(html)
			},
			error: function (list){
				alert("网络错误")
			}
		})

	}


	//增加
	function insert(){
		var selectbrand = $("#brandid").select2("data");
		var brands = []
		for (var i = 0; i < selectbrand.length; i++) {
			var brand = {id:selectbrand[i].id,text:selectbrand[i].text.trim()}
			brands.push(brand)
		}
		var brand = JSON.stringify(brands);

		var specs = []
		var selectspec = $("#specid").select2("data");
		for (var i = 0; i < selectspec.length; i++) {
			var spec = {id:selectspec[i].id,text:selectspec[i].text.trim()}
			specs.push(spec)
		}
		var spec = JSON.stringify(specs);

		var itemsTexts = []
		var trs = $("#itemsid").find("tr");
		for (var i = 1; i < trs.length; i++) {
			var tds = $(trs[i]).find("td");
			var itemsText = {text:$(tds[1]).find("input").val()}
			itemsTexts.push(itemsText)
		}
		var itemsText = JSON.stringify(itemsTexts);

		var templateList = {id:null,name:$("#name").val(),brandIds:brand,specIds:spec,customAttributeItems:itemsText}
		console.log(templateList)
		$.ajax({
			url:"/templateController/insertTemplate",
			type: "post",
			dataType: "json",
			data: {id:null,name:$("#name").val(),brandIds:brand,specIds:spec,customAttributeItems:itemsText},
			async: false,
			success:function (list){
				alert(list.codename)
				window.location.reload();
			},
			error: function (list){
				alert("网络错误")
			}
		})
	}

	//回显
	function echo(id){
		$.ajax({
			url:"/templateController/selectById?id=" + id,
			type: "post",
			dataType: "json",
			async: false,
			success:function (list){
				var tbTypeTemplate = list.t;
				$("#name2").val(tbTypeTemplate.name)
				var brand = tbTypeTemplate.brandIds;
				var brands = JSON.parse(brand);
				var spec = tbTypeTemplate.specIds;
				var specs = JSON.parse(spec);
				var brandids = []
				for (var i = 0; i < brands.length; i++) {
					brandids.push(brands[i].id)
				}
				var specids = []
				for (var i = 0; i < specs.length; i++) {
					specids.push(specs[i].id)
				}
				$("#brand").select2();
				$("#brand").val(brandids).trigger("change")
				$("#spec").select2();
				$("#spec").val(specids).trigger("change")

				var cuctom = tbTypeTemplate.customAttributeItems;
				var cuctoms = JSON.parse(cuctom);
				var html = ""
				for (var i = 0; i < cuctoms.length; i++) {
					// console.log(cuctoms[i].text)
					html += '<tr><td><input type="checkbox" className="icheckbox_square-blue"></td>'
					html += '<td><input value="' + cuctoms[i].text + '" className="form-control" placeholder="属性名称"></td>'
					html +=	'<td><button type="button" className="btn btn-default" title="删除"><iclassName="fa fa-trash-o"></i> 删除</button></td>' +
							'<td><input type="hidden" id="id" value="' + id + '" className="icheckbox_square-blue"></td></tr>'
				}

				$("#typename").html(html)
			},
			error: function (list){
				alert("网络错误")
			}
		})
	}

	//修改
	function update(){
		var selectbrand = $("#brand").select2("data");
		var brands = []
		for (var i = 0; i < selectbrand.length; i++) {
			var brand = {id:selectbrand[i].id,text:selectbrand[i].text.trim()}
			brands.push(brand)
		}
		var brand = JSON.stringify(brands);

		var specs = []
		var selectspec = $("#spec").select2("data");
		for (var i = 0; i < selectspec.length; i++) {
			var spec = {id:selectspec[i].id,text:selectspec[i].text.trim()}
			specs.push(spec)
		}
		var spec = JSON.stringify(specs);

		var itemsTexts = []
		var trs = $("#itemsid2").find("tr");
		for (var i = 1; i < trs.length; i++) {
			var tds = $(trs[i]).find("td");
			var itemsText = {text:$(tds[1]).find("input").val()}
			itemsTexts.push(itemsText)
		}
		var itemsText = JSON.stringify(itemsTexts);

		var templateList = {id:null,name:$("#name2").val(),brandIds:brand,specIds:spec,customAttributeItems:itemsText}
		console.log(templateList)
		$.ajax({
			url:"/templateController/updateTemplate",
			type: "post",
			dataType: "json",
			data: {id:$("#id").val(),name:$("#name2").val(),brandIds:brand,specIds:spec,customAttributeItems:itemsText},
			async: false,
			success:function (list){
				alert(list.codename)
				window.location.reload();
			},
			error: function (list){
				alert("网络错误")
			}
		})
	}

	//删除
	function deleteById(){
		var checks = document.getElementsByName("check");
		var ids = []
		for (var i = 0; i < checks.length; i++) {
			if (checks[i].checked){
				ids.push(checks[i].value)
			}
		}
		$.ajax({
			url:"/templateController/deleteById?ids=" + ids,
			type: "post",
			dataType: "json",
			async: false,
			success:function (list){
				alert(list.codename)
				window.location.reload();
			},
			error: function (list){
				alert("网络错误")
			}
		})
	}
</script>
<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" onclick="deleteById()" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input id="nameid" name="name" >
									<button onclick="findall()" class="btn btn-default">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="findall">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input id="name" class="form-control" placeholder="商品类型">  </td>
		      	</tr>
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
		      			<select id="brandid" multiple style="width: 100%">
							<th:block th:each="brand : ${tbBrands}">
								<option th:value="${brand.id}">
									[[${brand.name}]]
								</option>
							</th:block>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="specid" multiple style="width: 100%">
							<th:block th:each="spec : ${tbSpecification}">
								<option th:value="${spec.id}">
									[[${spec.specName}]]
								</option>
							</th:block>
						</select>
		      		</td>
		      	</tr>

		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

                        </div>
						<table class="table table-bordered table-striped"  width="800px" id="itemsid">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>
									<td>操作</td>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>

		      		</td>
		      	</tr>

			 </table>
			
		</div>
		<div class="modal-footer">						
			<button onclick="insert()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="editModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel2">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>商品类型</td>
						  <td><input id="name2" class="form-control" placeholder="商品类型">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="brand" multiple style="width: 100%">
								  <th:block th:each="brand : ${tbBrands}">
									  <option th:value="${brand.id}">
										  [[${brand.name}]]
									  </option>
								  </th:block>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="spec" multiple style="width: 100%">
								  <th:block th:each="spec : ${tbSpecification}">
									  <option th:value="${spec.id}">
										  [[${spec.specName}]]
									  </option>
								  </th:block>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px" id="itemsid2">
								  <thead>
									  <tr>
										  <td><input type="checkbox" class="icheckbox_square-blue"></td>
										  <td>属性名称</td>
										  <td>操作</td>
									  </tr>
								  </thead>
								  <tbody id="typename">

								  </tbody>
							  </table>

						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button onclick="update()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
    
</body>

</html>